<template>
  <div>
    <!-- <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
            <el-form-item> -->

    <!-- <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button> -->
    <!-- </el-form-item>
          </el-form> -->
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-form>
            <el-form-item label="设计师名称" prop="color">
              <el-input v-model="route.query.nickName" disabled />
            </el-form-item>
          </el-form> -->
        <el-button type="primary" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange">
      <el-table-column prop="message" label="留言信息" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="customerName" align="center" label="留言客户名称" v-if="isAdmin" />
      <el-table-column prop="createTime" label="留言时间" align="center" />
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="warning" size="small" icon="edit" title="编辑" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改Logo需求提交对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="留言信息" prop="message">
              <el-input type="textarea" v-model="form.message" placeholder="请输入留言信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="logorequiresub">
import { getCustomerFeedbackWithPage, addCustomerFeedback, updateCustomerFeedback, deleteCustomerFeedback } from '@/api/souvenir/customerFeedback'
import { useI18n } from 'vue-i18n'
import { getToken } from '@/utils/auth'
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
const { proxy } = getCurrentInstance()
const { t } = useI18n()
const route = useRoute()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const isAdmin = userStore.roles.includes('admin')
const isCustomer = userStore.roles.includes('customer')
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  customerId: isAdmin ? null : userStore.userId
})
console.log(userStore.roles)
const columns = ref([
  { visible: true, prop: 'id', label: 'Id' },
  { visible: true, prop: 'logoCategory', label: 'Logo类别' },
  { visible: true, prop: 'style', label: '风格' },
  { visible: true, prop: 'color', label: '颜色' },
  { visible: true, prop: 'element', label: '元素' },
  { visible: true, prop: 'imageId', label: '图片ID' },
  { visible: true, prop: 'designerName', label: '设计师ID' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

var dictParams = []

function getList() {
  loading.value = true
  getCustomerFeedbackWithPage(queryParams).then((res) => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order
  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    id: [{ required: true, message: 'Id不能为空', trigger: 'blur', type: 'number' }]
  },
  options: {}
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    message: null,
    customerId: userStore.userId
  }
  proxy.resetForm('formRef')
}
// 添加按钮操作
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加留言信息'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  if (row) {
    open.value = true
    title.value = '更新留言信息'
    opertype.value = 2
    form.value = row
  }
}

const handleDelete = (row) => {
  proxy.$modal
    .confirm('是否确认删除?')
    .then(async () => {
      try {
        const { data } = await deleteCustomerFeedback(row.id)
        if (data) {
          proxy.$modal.msgSuccess('删除成功')
          getList()
        }
      } catch (error) {}
    })
    .catch(() => {})
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined && opertype.value === 2) {
        updateCustomerFeedback(form.value).then((res) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addCustomerFeedback(form.value).then((res) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

handleQuery()
</script>
